package com.xzq.erp.service.impl;

import com.xzq.erp.enums.ResponseCodeEnum;
import com.xzq.erp.exception.BizException;
import com.xzq.erp.service.IFileService;
import com.xzq.erp.utils.MinioUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class FileServiceImpl implements IFileService {

    private final MinioUtil minioUtil;

    @Override
    public String fileUpload(MultipartFile file) {
        try{
            return minioUtil.uploadFile(file);
        }
        catch (Exception e){
            log.error("文件上传失败", e);
            throw new BizException(ResponseCodeEnum.FILE_UPLOAD_ERROR);
        }
    }

    @Override
    public void deleteFile(String fileName) {
        try{
            minioUtil.deleteFile(fileName);
        }
        catch (Exception e){
            log.error("文件删除失败", e);
            throw new BizException(ResponseCodeEnum.FILE_DELETE_ERROR);
        }
    }

    @Override
    public void deleteFiles(List<String> fileNames) {
        try{
            minioUtil.deleteFiles(fileNames);
        }
        catch (Exception e){
            log.error("文件删除失败", e);
            throw new BizException(ResponseCodeEnum.FILE_DELETE_ERROR);
        }
    }
}
